package io.kiki.stack.http.feign.fastjson2;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import feign.RequestTemplate;
import feign.Util;
import feign.codec.EncodeException;
import feign.codec.Encoder;

import java.lang.reflect.Type;

/**
 * @author changjin wei(魏昌进)
 */
public class Fastjson2Encoder implements Encoder {

    private final JSONWriter.Feature[] features;

    public Fastjson2Encoder() {
        this(new JSONWriter.Feature[0]);
    }

    public Fastjson2Encoder(JSONWriter.Feature[] features) {
        this.features = features;
    }

    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        template.body(JSON.toJSONBytes(object, features), Util.UTF_8);
    }
}
